package org.crag.cafca.gui.servlet;

import java.io.IOException;
import java.util.SortedSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.crag.cafca.logic.time.Event;
import org.crag.cafca.logic.time.FamilyEvent;
import org.crag.cafca.logic.time.SourceEvent;
import org.crag.cafca.logic.time.SourceManagementEvent;
import org.crag.cafca.logic.time.TimeLine;
import org.crag.cafca.util.Utilities;
import org.joda.time.DateTime;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Servlet used to see every TimeEvent of the current User Timeline. 
 * Create a JSONObject containing the data. 
 *
 */
@WebServlet("/SeeEveryTimeEventServlet")
public class SeeEveryTimeEventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    public SeeEveryTimeEventServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = (HttpSession) request.getSession(); 
		TimeLine timeline = (TimeLine) session.getAttribute("timeline"); 
		
		JSONObject resp = createJson(timeline); 
		response.getWriter().print(resp.toString());
		response.setStatus(HttpServletResponse.SC_OK);
	}
	
	
	private JSONObject createJson(TimeLine timeline){
		JSONObject toSendBack = new JSONObject(); 
		JSONArray dataArray = new JSONArray(); 
		SortedSet<Event> events = timeline.getEvents(); 
		
		for(Event event : events)
		{
			JSONObject row = new JSONObject(); 
			
			String description=event.getDescription();
			if (description!=null)
				row.put("description", event.getDescription()); 

			DateTime startDateTime=event.getStartDateTime();
			if (startDateTime!=null)
				row.put("startDateTime", Utilities.toJSONDateTime(startDateTime));

			DateTime endDateTime=event.getStartDateTime();			
			if (endDateTime!=null)
				row.put("endDateTime", Utilities.toJSONDateTime(endDateTime));
				
			String tags=event.getTagString();
			if (tags!=null)
				row.put("tags", tags);

			String value=event.getValue();
			if (tags!=null)
				row.put("value", value);
			
			if (event instanceof SourceEvent)
			{
				SourceEvent sourceEvent=(SourceEvent)event;
				row.put("name", sourceEvent.getSourceId()); 
			}

			if (event instanceof SourceManagementEvent)
			{
				SourceManagementEvent sourceManagementEvent=(SourceManagementEvent)event;
				row.put("name", sourceManagementEvent.getSourceId()); 
			}

			if (event instanceof FamilyEvent)
			{
				FamilyEvent familyEvent=(FamilyEvent)event;
			}			

			dataArray.put(row); 
		}
		
		toSendBack.put("data", dataArray); 
		toSendBack.put("success", true); 
		
		return toSendBack; 
	}

}
